/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package cn.eclass.dao;

import cn.eclass.entity.AdCategoryEntity;
import cn.eclass.entity.Entity;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author fortruth
 */
public class AdCategoryDao extends CommonDao {

    private static String TABLE_NAME = "adcategory";

    public AdCategoryDao() {
        super(AdCategoryDao.class.getName());
    }

    public List<AdCategoryEntity> getList() {
        List<AdCategoryEntity> adcategorys = new ArrayList<AdCategoryEntity>();
        String sql = "select * from adcategory";
        adcategorys = (List<AdCategoryEntity>) select(sql);
        return adcategorys;
    }

    @Override
    protected Entity fillEntity(ResultSet resultSet) {

        AdCategoryEntity adCategoryEntity = null;
        try {
            adCategoryEntity = new AdCategoryEntity();
            adCategoryEntity.setId(resultSet.getInt("ID"));
            adCategoryEntity.setName(resultSet.getString("NAME"));
            adCategoryEntity.setUsage(resultSet.getInt("USAGE"));
            adCategoryEntity.setDescription(resultSet.getString("DESCRIPTION"));

        } catch (SQLException ex) {
            logger.error(ex);
        }
        return adCategoryEntity;
    }

    public AdCategoryEntity create(AdCategoryEntity adCategoryEntity) {
        String sql = "insert into adcategory (`name`,`description`,`usage`) values "
                + "('" + adCategoryEntity.getName() + "','" + adCategoryEntity.getDescription() + "'," + adCategoryEntity.getUsage() + ")";
        int key = super.insert(sql);
        adCategoryEntity.setId(key);
        return adCategoryEntity;

    }

    public boolean delete(int adCatId) {
        String sql = "delete from " + TABLE_NAME + " where id = " + adCatId;
        int effectedRows = super.delete(sql);
        if (effectedRows > 0) {
            return true;
        } else {
            return false;
        }
    }

    public boolean update(AdCategoryEntity adCategoryEntity) {
        String sql = "update " + TABLE_NAME + " set "
                + "`NAME` = '" + adCategoryEntity.getName() + "',"
                + "`USAGE` = " + adCategoryEntity.getUsage() + ","
                + "`DESCRIPTION` = '" + adCategoryEntity.getDescription() + "'"
                + " where ID = " + adCategoryEntity.getId();
        int effectedRows = super.delete(sql);
        if (effectedRows > 0) {
            return true;
        } else {
            return false;
        }
    }

    public AdCategoryEntity select(int theId) {
        AdCategoryEntity entity = null;
        String sql = "select * from " + TABLE_NAME + " where id = " + theId;
        List<AdCategoryEntity> entities = (List<AdCategoryEntity>) super.select(sql);
        if (entities.size() > 0) {
            entity = entities.get(0);
        }
        return entity;
    }

    public AdCategoryEntity add(AdCategoryEntity entity) {
        String sql = "insert into " + TABLE_NAME
                + " (`NAME`,`USAGE`,`DESCRIPTION`)"
                + " values "
                + " ("
                + "'" + entity.getName() + "',"
                + entity.getUsage() + ","
                + "'" + entity.getDescription() + "'"
                + ")";
        int lastId = super.insert(sql);
        entity.setId(lastId);
        return entity;
    }
}
